package ansi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import ansi.exception.DAOExcepcion;
import ansi.modelo.AreaComun;
import ansi.modelo.Vivienda;
import ansi.util.ConexionBD;
import ansi.util.Constantes;

public class AreaComunDAO  extends BaseDAO {
	
	
	public AreaComun obtener(int idArea_Comun) throws DAOExcepcion {
		AreaComun vo = new AreaComun();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "SELECT idArea_Comun, Descripcion_Area_Comun ";
			query += "FROM Area_Comun WHERE idArea_Comun = ? ";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idArea_Comun);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdArea_Comun(rs.getInt("idArea_Comun"));
				vo.setDescripcion_Area_Comun(rs.getString("Descripcion_Area_Comun"));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<AreaComun> listar() throws DAOExcepcion {
		Collection<AreaComun> c = new ArrayList<AreaComun>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idArea_Comun, Descripcion_Area_Comun ";
			query += "FROM Area_Comun ORDER BY Descripcion_Area_Comun ";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				AreaComun vo = new AreaComun();
				vo.setIdArea_Comun(rs.getInt("idArea_Comun"));
				vo.setDescripcion_Area_Comun(rs.getString("Descripcion_Area_Comun"));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	public void insertar(AreaComun vo) throws DAOExcepcion {
		String query = "INSERT INTO Area_Comun ";
		query += "(Descripcion_Area_Comun) ";
		query += "VALUES (?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getDescripcion_Area_Comun());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdArea_Comun(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	public AreaComun actualizar(AreaComun vo) throws DAOExcepcion {
		String query = "UPDATE Area_Comun SET ";
		query += "Descripcion_Area_Comun = ? ";
		query += "WHERE idArea_Comun = ? ";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(2, vo.getIdArea_Comun());
			stmt.setString(1, vo.getDescripcion_Area_Comun());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public void eliminar(int idAreaComun) throws DAOExcepcion {
		String query = "DELETE FROM Area_Comun WHERE idArea_Comun = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idAreaComun);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
}
